
let my_string_of_float = string_of_float

let (entete : out_channel -> string -> string -> unit) =
  fun oc cb ce -> 
    let time = Unix.localtime (Unix.time ()) in
     let sys_call, _ = Array.fold_left  
       (fun (acc,i) x -> 
         if 70 < i + (String.length x) then  
 	       acc ^ ce^ "\n"^cb^"\t\t" ^ x, String.length ("\n  \t\t" ^ x) 
         else  
 	       acc ^ " " ^ x , (i+1+(String.length x)) 
       ) 
       ("",0)
       Sys.argv 
     and date = Printf.sprintf "%02d/%02d/%d"
      (time.Unix.tm_mday)
      (time.Unix.tm_mon+1)
      (1900+time.Unix.tm_year)
    and
        time_str = Printf.sprintf "%02d:%02d:%02d"
      (time.Unix.tm_hour)
      (time.Unix.tm_min)
      (time.Unix.tm_sec) 
   (*   and user = Unix.getlogin () *)
    and hostname = Unix.gethostname ()
    in
      (* Printf.fprintf oc "-- lv6 version %s\n" LustreVersion.str; *)
      (* Printf.fprintf oc "-- cmd: %s\n" sys_call; *)
      (* Printf.fprintf oc "-- host: %s date: %s time: %s\n" hostname date time_str  *)
    Printf.fprintf oc "%s This file was generated by lv6 version %s. %s\n"
      cb Lv6version.str ce;
    Printf.fprintf oc "%s %s %s\n" cb sys_call ce;
    Printf.fprintf oc "%s on %s the %s at %s %s\n" cb hostname date time_str ce


let rec pos_in_list i x l =
  match l with
    | e::l -> if e=x then i else pos_in_list (i+1) x l
    | [] -> assert false (* should not occur *)

let my_int_of_string = LocalGenlex.local_int_of_string

let string_ends str1 str2 =
  let l1,l2 = String.length str1, String.length str2 in
  l1>=l2 &&
    str2 = String.sub str1 (l1 - l2 ) l2

let _ = assert (string_ends "int []" "[]")

(* [my_assoc x l] returns the elt associated to x in l plus l without (x,elt) *)
let my_assoc x l = 
  let rec aux acc = function
  | [] -> None
  | (a,b)::l -> if compare a x = 0 then Some(b,List.rev_append acc l) else aux ((a,b)::acc) l
  in 
  aux [] l


let gen_N i = 
  let rec aux acc n = if n<0 then acc else aux (n::acc) (n-1) in
  aux [] i
let _ = assert (gen_N 10 = [0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10])
